//
//  ChangViewController.m
//  cherryproduct
//
//  Created by Enter zhao on 14-8-13.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "ChangViewController.h"

@interface ChangViewController ()<UIAlertViewDelegate>

@property(nonatomic, strong) UILabel * label_press;

@end

@implementation ChangViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _textview.delegate = self;
    _textview.font = [UIFont systemFontOfSize:18];
    _textview.textColor = [UIColor colorWithRed:(51.0f/255.0f) green:(51.0f/255.0f) blue:(51.0f/255.0f) alpha:1.0];
    _textview.backgroundColor = [UIColor whiteColor];
    _textview.layer.borderWidth = 0.5;
    _textview.layer.borderColor = [[UIColor colorWithRed:(229.0f/255.0f) green:(229.0f/255.0f) blue:(229.0f/255.0f) alpha:1.0] CGColor];
    _textview.returnKeyType = UIReturnKeyDefault;//返回键的类型
    _textview.keyboardType = UIKeyboardTypeDefault;//键盘类型
    _textview.scrollEnabled = YES;//是否可以拖动
    _textview.delegate = self;
    _textview.userInteractionEnabled = YES;
    [self.myView addSubview:_textview];
    [_textview becomeFirstResponder];
    
    [self setTitle:strTitle];
    [self addNavBarButton:RIGHT_FINISH];
    
    {
        self.label_press = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 38)];
        self.label_press.font=[UIFont systemFontOfSize:18];
        self.label_press.backgroundColor=[UIColor clearColor];
        self.label_press.textColor=[UIColor grayColor];
        self.label_press.text=@"请输入...";
        [self.myView addSubview:self.label_press];
    }
    if (self.str_show_info) {
        _textview.text=self.str_show_info;
        self.label_press.hidden=YES;
    }
    if ([strTitle isEqualToString:@"消息提醒时间"])
    {
        _textview.keyboardType=UIKeyboardTypeNumberPad;
    }
    
    //键盘
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)setLabel:(UILabel*)label title:(NSString*)title
{
    tempLabel = label;
    strTitle = [[NSString alloc] initWithString:title];
}

- (void)finishButtonAction:(UIButton *)sender
{
    sender.enabled = NO;
    
    if (_textview.text == nil || [_textview.text isEqualToString:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请填写内容"];
        
        sender.enabled = YES;
        
        return;
    }
    
    [self.view startLoadingAnimation];
    
    if ([strTitle isEqualToString:@"意见反馈"]) {
        NSMutableDictionary * dic_info = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_textview.text, @"content", nil];
        [[HttpNewBaseService sharedClient] postRequestOperation:@"member/feedback"
                                                paramDictionary:dic_info
                                                   onCompletion:^(NSString *responseString) {
                                                       [self.view stopLoadingAnimation];
                                                       
                                                       NSDictionary * dic = [responseString JSONValue];
                                                       if (dic) {
                                                           NSInteger status = [[dic objectForKey:@"status"] integerValue];
                                                           if (status==1) {
                                                               /*
                                                               UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"意见反馈成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                                               [alert show];
                                                                */
                                                               [self goBackPage];
                                                               return;
                                                           }
                                                       }
                                                       sender.enabled = YES;
                                                       [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                                                   } onError:^(NSError *error) {
                                                       [self.view stopLoadingAnimation];
                                                       
                                                       sender.enabled = YES;
                                                       [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
                                                   } onAnimated:YES];
        return;
    }

    NSMutableDictionary* dic = nil;
    if ([strTitle isEqualToString:@"昵称"]) {
        dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               _textview.text, @"nickname", nil];
    } else if ([strTitle isEqualToString:@"签名"])
    {
        dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               _textview.text,@"sign",nil];
    } else if ([strTitle isEqualToString:@"消息提醒时间"])
    {
        dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               _textview.text,@"warning",nil];
    }
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/set_profile"
                                            paramDictionary:dic
                                               onCompletion:^(NSString *responseString) {
                                                   [self.view stopLoadingAnimation];
                                                   
                                                   NSDictionary * dic = [responseString JSONValue];
                                                   if (dic) {
                                                       NSInteger status = [[dic objectForKey:@"status"] integerValue];
                                                       if (status==1) {
                                                           /*
                                                           UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                                           [alert show];
                                                            */
                                                           [self goBackPage];
                                                           return;
                                                       }
                                                   }
                                                   sender.enabled = YES;
                                                   [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        //strTitle = nil;
        //tempLabel.text = _textview.text;
        
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        //strTitle = nil;
        //tempLabel.text = _textview.text;
        //[self.navigationController popViewControllerAnimated:YES];
        sender.enabled = YES;
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)goBackPage
{
    if (self.block_dismiss) {
        self.block_dismiss(self.textview.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (self.block_dismiss) {
        self.block_dismiss(self.textview.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.label_press.hidden=YES;
    } else {
        self.label_press.hidden=NO;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    NSString * temp = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (temp.length > 50) {
        textView.text = [temp substringToIndex:50];
        return NO;
    }
    return YES;
}

-(void)keyboardWillShow:(NSNotification *)notification
{
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
        CGRect keyboardBounds;
        [keyboardBoundsValue getValue:&keyboardBounds];
        NSInteger offset = keyboardBounds.origin.y;
        NSLog(@"offset is %f",keyboardBounds.origin.y);
        
        if ([[UIDevice currentDevice].systemVersion doubleValue] < 7.0) {
            offset=offset-20;
        }
        
        [UIView beginAnimations:@"anim" context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.1];
        //处理移动事件，将各视图设置最终要达到的状态
        CGFloat height = offset - 64;
        
        self.textview.frame=CGRectMake(0, 0, self.myView.frame.size.width, height);
        
        [UIView commitAnimations];
    }
}

@end

























